package com.bungarus.om.controller;

import com.bungarus.cluster.ClusterManager;
import com.bungarus.model.RetryFailedMessage;
import com.bungarus.offline.OfflineMessage;
import com.bungarus.om.busi.OfflineMessageManager;
import org.apache.log4j.Logger;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/offline/v1.0")
public class OfflineController extends BungarusController{

    Logger logger = Logger.getLogger(ClusterManager.class);

    private OfflineMessageManager offlineMessageManager = new OfflineMessageManager();

    @GetMapping("/messages")
    public ResponseEntity<List<RetryFailedMessage>> doGetMessages(@RequestParam(name = "tid", required = true) int tenantId,
                                                      @RequestParam(name = "uid", required = true) String userId,
                                                      @RequestParam(name = "app", required = true) String app,
                                                      @RequestParam(name = "dev", required = false, defaultValue="") String dev,
                                                      @RequestParam(name = "offset", required = true) int offset,
                                                      @RequestParam(name = "size", required = true) int size,
                                                      @RequestParam(name = "peek", required = false, defaultValue = "0") int peek) {

        List<OfflineMessage> offlineMessages = offlineMessageManager.retriveOfflineMessages(tenantId, userId, app, dev, offset, size);
        List<RetryFailedMessage> messages = new ArrayList<>();
        offlineMessages.forEach(offlineMessage -> {
            messages.add(offlineMessage.getMessage());
        });

        //update pointer
        if(offlineMessages.size() > 0) {
            //retrieve messages from old to new
            long offlineIdMax = offlineMessages.get(offlineMessages.size()-1).getOfflineId();
            if(1 != peek) { // if peek=1, means just to take a look for the data, which won't update the pointer's value
                offlineMessageManager.updateOfflineMessagePointer(tenantId, userId, app, dev, offlineIdMax);
            }
            /*
            //retrieve messages from new to old, two problems:
            //1. may the requests be interrupted before retrieve all messages, then duplicated messages would be pulled
            //2. may the new offline message generated during the requests, then the newest offlinePointer would be wrong
            long offlineIdMin = offlineMessages.get(offlineMessages.size()-1).getOfflineId();
            long currentOfflinePointerOffset = offlineMessageManager.getCurrentOfflineMessagePointerOffset(tenantId, userId, dev);
            if(offlineIdMin <= currentOfflinePointerOffset){
                //update offlineId with max current id
                //offlineMessageManager.updateOfflineMessagePointer(tenantId, userId, dev, );
            }
            */
        }

        return ResponseEntity.ok()
                .cacheControl(CacheControl.noCache())
                .header("Pragma", "no-cache")
                .body(messages);
    }


}
